package com.dq.service.sys.user.centre.impl;

import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.controller.in.user.UserAnonymousQuery;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.centre.UserAnonymous;
import com.dq.exception.customException.AdminException;
import com.dq.repository.BaseRepositories;
import com.dq.service.sys.user.centre.UserAnonymousService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 */
@Service
public class UserAnonymousServiceImpl extends BaseRepositories implements UserAnonymousService {

    @Override
    public List<UserAnonymous> list(UserAnonymousQuery userAnonymousQuery) throws AdminException {
        userAnonymousQuery.format();
        //测试
        //userAnonymousQuery.setSectionId(0L);
        Map<String, Object> query = createQuery(userAnonymousQuery);

        return dao.find(dao.query2Hql(UserAnonymous.class, query), query);
    }

    @Override
    public Long count(UserAnonymousQuery userAnonymousQuery) throws AdminException {
        userAnonymousQuery.format();
        Map<String, Object> query = createQuery(userAnonymousQuery);

        return dao.count(dao.count2Hql(UserAnonymous.class, query), query);
    }

    public Map<String, Object> createQuery(UserAnonymousQuery userAnonymousQuery) throws AdminException {
        // TODO: 2020/8/3 0003 测试
        //userAnonymousQuery.setSectionId(0L);
        Map<String, Object> query = userAnonymousQuery.parseMap();
        if (userAnonymousQuery.getSectionId() != null && userAnonymousQuery.getSectionId() != 0) {
            query.put("section_id", userAnonymousQuery.getSectionId());
            query.remove("sectionId");
        } else if (userAnonymousQuery.getSectionId() == 0) {//如果传过来的SectionId为0
            query.remove("sectionId");
        } else {
            throw new AdminException("未选择门店");
        }
        if (userAnonymousQuery.getCreateUserId() != null) {
            query.put("createUser_id", userAnonymousQuery.getCreateUserId());
            query.remove("createUserId");
        }
        if (StringUtils.isNotBlank(userAnonymousQuery.getCreateUserName())) {
            query.put("createUser_userRealcName" + BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(userAnonymousQuery.getCreateUserName()));
            query.remove("createUserName");
        }

        return query;
    }


    @Override
    public UserAnonymous save(@Valid UserAnonymous userAnonymous) {
        verifyObject(userAnonymous);

        SysUser loginUser = sysUserService.getUser();
        if (loginUser.getSection() == null || loginUser.getSection().getId() == null) {
            throw new AdminException("登录账号所属门店信息异常");
        }
        userAnonymous.setSection(loginUser.getSection());

        userAnonymous.setCreateUser(loginUser);
        userAnonymous.setUpdateUser(loginUser);

        return userAnonymousRepository.save(userAnonymous);
    }


    public void verifyObject(UserAnonymous userAnonymous) throws AdminException {
    }

}
